package com.qcby.llmtchstd.utils;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.regex.Pattern;

public class BrowserUtils {
    private static final Pattern IE_PATTERN = Pattern.compile("MSIE|Trident|Edge");

    public static String encodeFileName(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
        String userAgent = request.getHeader("User-Agent");
        if (userAgent != null && IE_PATTERN.matcher(userAgent).find()) {
            // IE/Edge 用 GBK 编码（避免中文乱码）
            return URLEncoder.encode(fileName, "GBK");
        } else {
            // 其他浏览器用 UTF-8 编码（标准格式）
            return "UTF-8''" + URLEncoder.encode(fileName, "UTF-8");
        }
    }
}